all files / table/ tr.vue

20.34% Statements 12/59
9.52% Branches 4/42
4.55% Functions 1/22
13.33% Lines 6/45
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145                            1× 1× 1× 1× 1×                                                                                                                                                                                                                                                            
//
//
//
//
//
//
//
//
//
//
//
//
//
 
import {isPc} from '../utils'
import td from './td'
import th from './th'
import checkbox from '../checkbox'
let uuid = 1
export default {
  name: 'mu-tr',
  props: {
    selectable: {
      type: Boolean,
      default: true
    },
    selected: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      hover: false,
      rowId: 'tr-' + uuid++
    }
  },
  mounted () {
    if (this.selected) {
      this.$parent.selectRow(this.rowId)
    }
  },
  computed: {
    className () {
      return {
        hover: this.hover,
        selected: this.isSelected,
        stripe: false
      }
    },
    isTh () {
      return this.$parent.isThead
    },
    isTf () {
      return this.$parent.isTfoot
    },
    isTb () {
      return this.$parent.isTbody
    },
    isSelected () {
      return this.$parent.selectedRows && this.$parent.selectedRows.indexOf(this.rowId) !== -1
    },
    showCheckbox () {
      return this.$parent.showCheckbox
    },
    enableSelectAll () {
      return this.$parent.enableSelectAll
    },
    multiSelectable () {
      return this.$parent.multiSelectable
    },
    isSelectAll () {
      return this.$parent.isSelectAll
    }
  },
  methods: {
    handleHover (event) {
      if (isPc() && this.$parent.isTbody) {
        this.hover = true
        if (this.$parent.handleRowHover) this.$parent.handleRowHover(event, this.rowId, this)
      }
    },
    handleExit (event) {
      if (isPc() && this.$parent.isTbody) {
        this.hover = false
        if (this.$parent.handleRowHoverExit) this.$parent.handleRowHoverExit(event, this.rowId, this)
      }
    },
    handleClick (e) {
      if (!this.$parent.isTbody) return
      if (this.selectable) {
        if (!this.isSelected) {
          this.$parent.selectRow(this.rowId)
        } else {
          this.$parent.unSelectRow(this.rowId)
        }
      }
      this.$parent.handleRowClick(e, this)
    },
    handleCheckboxClick (event) {
      event.stopPropagation()
    },
    handleCheckboxChange (val) {
      if (!this.selectable) return
      if (val) {
        this.$parent.selectRow(this.rowId)
      } else {
        this.$parent.unSelectRow(this.rowId)
      }
    },
    handleSelectAllChange (val) {
      if (val) {
        this.$parent.selectAll()
      } else {
        this.$parent.unSelectAll()
      }
    },
    handleCellHover (event, name, td) {
      if (this.$parent.handleCellHover) this.$parent.handleCellHover(event, name, td, this.rowId, this)
    },
    handleCellHoverExit (event, name, td) {
      if (this.$parent.handleCellHoverExit) this.$parent.handleCellHoverExit(event, name, td, this.rowId, this)
    },
    handleCellClick (event, name, td) {
      if (this.$parent.handleCellClick) this.$parent.handleCellClick(event, name, td, this.rowId, this)
    }
  },
  watch: {
    selected (val, oldVal) {
      if (val === oldVal) return
      if (val) {
        this.$parent.selectRow(this.rowId)
      } else {
        this.$parent.unSelectRow(this.rowId)
      }
    }
  },
  components: {
    'mu-td': td,
    'mu-th': th,
    checkbox
  }
}